<?php

// +----------------------------------------------------------------------
// | Shop-Demo for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2022~2023 Anyon <zoujingli@qq.com>
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// | 会员免费 ( https://thinkadmin.top/vip-introduce )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\data\controller;

use app\data\model\DataXinUser;
use think\admin\Controller;
use think\admin\helper\QueryHelper;

/**
 * 用户管理
 * Class Cate
 * @package app\data\controller\shop
 */
class User extends Controller
{

    /**
     * 用户列表管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        DataXinUser::mQuery()->layTable(function () {
            $this->title = "用户列表管理";
        }, function (QueryHelper $query) {
            $query->like('name')->dateBetween('create_at');
        });
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(array &$data)
    {
    }

    /**
     * 添加服务事件
     * @auth true
     */
    public function add()
    {
        DataEapEvent::mForm('form');
    }

    /**
     * 编辑服务事件
     * @auth true
     */
    public function edit()
    {
        DataEapEvent::mForm('form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isGet()) {
            if(!empty($data['site_info'])){$data['site_infos'] = explode(',',$data['site_info']);}else{$data['site_info'] = '';}
        }else{
            if(!isset($data['id'])){$data['create_at'] = date('Y-m-d H:i:s');}
            if(!empty($data['site_info'])){
                $is_site_info = $this->getSite($data);
                $data = $is_site_info;
                $data['site_info'] = implode(',',$data['site_info']);
            }
            //print_r($data);die;

        }
    }

    protected function getSite($data){
        $data['is_site_cs'] = 0;
        $data['is_site_nc'] = 0;
        $data['is_site_hy'] = 0;
        $data['is_site_nl'] = 0;
        $data['is_site_hf'] = 0;
        $data['is_site_gz'] = 0;
        $data['is_site_total'] = 0;
        foreach ($data['site_info'] as $v){
            switch ($v){
                case '长沙':
                    $data['is_site_cs'] = 1;
                    break;
                case '南昌':
                    $data['is_site_nc'] = 1;
                    break;
                case '衡阳':
                    $data['is_site_hy'] = 1;
                    break;
                case '南宁':
                    $data['is_site_nl'] = 1;
                    break;
                case '合肥':
                    $data['is_site_hf'] = 1;
                    break;
                case '广州':
                    $data['is_site_gz'] = 1;
                    break;
                default:
                    $data['is_site_total'] = 1;
            }
        }
        return $data;
    }

    /**
     * 修改服务事件状态
     * @auth true
     */
    public function state()
    {
        DataEapEvent::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 表单结果处理
     * @param boolean $state
     */
    protected function _form_result(bool $state)
    {
        if ($state) {
            $this->success('操作成功！', 'javascript:history.back()');
        }
    }

    /**
     * 删除服务事件
     * @auth true
     */
    public function remove()
    {
        DataEapEvent::mDelete();
    }
}